package com.halixun.d2app.ui;


import android.hardware.usb.UsbDevice;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.recyclerview.widget.RecyclerView;

import com.halixun.d2app.R;
import com.halixun.d2app.adapter.PrintListAdapter;
import com.halixun.d2app.base.BaseActivity;
import com.halixun.d2app.base.BasePrintInfo;
import com.halixun.d2app.bean.print.NetPrintInfo;
import com.halixun.d2app.bean.print.UsbPrintInfo;
import com.halixun.d2app.printer.USBPrinter;
import com.halixun.d2app.utils.PrintReceiptsUtil;

import java.util.ArrayList;
import java.util.List;

public class PrintListActivity extends BaseActivity implements USBPrinter.UsbPrintListener {

    private RecyclerView mRecyclerView;

    private List<BasePrintInfo> mInfos;

    private PrintListAdapter mAdapter;

    private USBPrinter usbPrinter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_list);
        hideActionBar();
        usbPrinter = USBPrinter.getInstance();
        if (!usbPrinter.getInitState()) {
            USBPrinter.initPrinter();
        }
        initView();
        setAdapter();
        setListeners();
        getData();
    }

    private void setListeners() {
        usbPrinter.setUsbPrintListener(this);
    }

    private void getData() {
        List<NetPrintInfo> netPrintInfos = PrintReceiptsUtil.getNetPrint();
        if (netPrintInfos != null) {
            mInfos.addAll(netPrintInfos);
        }
        List<UsbPrintInfo> usbPrintInfos = PrintReceiptsUtil.getUsbPrint();
        if (usbPrintInfos != null) {
            List<UsbDevice> devices = usbPrinter.getConnectDevice();
            for (UsbPrintInfo info : usbPrintInfos) {
                info.isConnect = false;
                for (UsbDevice device : devices) {
                    if (device.getDeviceName().equals(info.id) && device.getManufacturerName().equals(info.name)) {
                        info.isConnect = true;
                    }
                }
            }
            mInfos.addAll(usbPrintInfos);
        }
        mAdapter.notifyDataSetChanged();
    }

    private void setAdapter() {
        mInfos = new ArrayList<>();
        mAdapter = new PrintListAdapter(mInfos);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        usbPrinter.setUsbPrintListener(null);
    }

    @Override
    public void onAddUsbPrint(UsbPrintInfo usbDevice) {
//        boolean isHas = true;
//        int index = 0;
//        for (BasePrintInfo info : mInfos) {
//            if (info instanceof UsbPrintInfo) {
//                UsbPrintInfo data = (UsbPrintInfo) info;
//                if (usbDevice.getDeviceName().equals(data.id) && usbDevice.getManufacturerName().equals(data.name)) {
//                    isHas = true;
//                    index = mInfos.indexOf(data);
//                }
//            }
//        }
//        if (isHas) {
//            ((UsbPrintInfo) mInfos.get(index)).isConnect = true;
//            mAdapter.notifyItemChanged(index);
//        } else {
//            UsbPrintInfo info = new UsbPrintInfo();
//            info.isConnect = true;
//            info.id = usbDevice.getDeviceName();
//            info.name = usbDevice.getManufacturerName();
//            mInfos.add(info);
//            mAdapter.notifyItemInserted(mInfos.size() - 1);
//        }
    }

    @Override
    public void onRemoveUsbPrint(UsbPrintInfo usbDevice) {
//        for (BasePrintInfo info : mInfos) {
//            if (info instanceof UsbPrintInfo) {
//                UsbPrintInfo data = (UsbPrintInfo) info;
//                if (data.id == usbDevice.getDeviceName() && data.name.equals(usbDevice.getManufacturerName())) {
//                    data.isConnect = false;
//                    mAdapter.notifyItemChanged(mInfos.indexOf(data));
//                }
//            }
//        }
    }

}
